
/*    This file is part of the Managarm operating system.
 *   Copyright (C) 2007, 2008, 2009  Alexander van der Grinten
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package org.managarm.jmanavalib.core;

public final class VaFldInfo extends VaObject {
	public static final int FLAG0_STATIC = 1;
	
	// identifier of this field
	public final String ident;
	
	// class of this field
	public final VaClsInfo clsInfo;
	
	// flags of this field
	public final int flags0;
	
	// datatype of this field
	public final VaDatatype datatype;
	
	public final VaLiteral literal;
	
	public VaFldInfo(String ident, VaClsInfo clsinfo,
			int flags0, VaDatatype datatype, VaLiteral literal) {
		this.ident = ident;
		this.clsInfo = clsinfo;
		this.flags0 = flags0;
		this.datatype = datatype;
		this.literal = literal;
	}

	// returns true if this field is compatible to "other"
	public boolean compatible(VaFldInfo other) {
		if(!ident.equals(other.ident))
			return false;
		if(!datatype.compatible(other.datatype))
		if((flags0 & FLAG0_STATIC) != (flags0 & FLAG0_STATIC))
			return false;
		return true;
	}
	
	// builds a hash code based on fields
	// significant for compatibility
	public int compatibilityHash() {
		int hash = 17;
		hash = 37 * hash + ident.hashCode();
		hash = 37 * hash + datatype.compatibilityHash();
		hash = 37 * hash + ((flags0 & FLAG0_STATIC) != 0 ? 0 : 1);
		return hash;
	}

	@Override public boolean equals(Object object) {
		if(object instanceof VaFldInfo == false)
			return false;
		final VaFldInfo other = (VaFldInfo)object;
		if(!ident.equals(other.ident))
			return false;
		if(!clsInfo.equals(other.clsInfo))
			return false;
		if(!datatype.compatible(other.datatype))
		if((flags0 & FLAG0_STATIC) != (flags0 & FLAG0_STATIC))
			return false;
		return true;
	}
	
	@Override public int hashCode() {
		int hash = 17;
		hash = 37 * hash + ident.hashCode();
		hash = 37 * hash + clsInfo.hashCode();
		hash = 37 * hash + datatype.compatibilityHash();
		hash = 37 * hash + ((flags0 & FLAG0_STATIC) != 0 ? 0 : 1);
		return hash;
	}
}
